﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
using System.Security.Principal;
using System.Security;

using Revis.VClinicNet.Core.Domain.Credentials;

namespace Revis.VClinicNet.UI.Internal.Mvc.Infrastructure
{
    public class AuthorizeFor : ActionFilterAttribute
    {
        public WorkerUserRole Role { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            IPrincipal user = filterContext.HttpContext.User;
            if (user.Identity.IsAuthenticated == false) {
                FormsAuthentication.RedirectToLoginPage();
            }
            string[] userRoles = Roles.GetRolesForUser();
            foreach (string userRole in userRoles) {
                WorkerUserRole role = (WorkerUserRole)Enum.Parse(typeof(WorkerUserRole), userRole);
                WorkerUserRole r = Role & role;
                if ((Role & role) != role) {
                    FormsAuthentication.RedirectToLoginPage();
                }
            }            
        }
    }
}
